const express=require('express')
const router=express.Router()
const db=require('../db/index')
const jwt=require('jsonwebtoken')
const secretKey='zhouzhichen'

router.post('/login',function(req,res) {
    console.log(req.query);
    const token=jwt.sign({username:req.body.name},secretKey,{expiresIn:'1h'})
    const info = req.body
    if(!info.name) {
        res.send({
            code:400,
            message:'参数错误'
        })
        return
    }
    db(`select * from patient where name='${info.name}'`).then(data=> {
        console.log(data);
        if(data.length==0 || data.code==500) {
            res.send({
                code:500,
                message:'用户不存在'
            })
            return
        }
        res.send({
            code:200,
            message:'登录成功',
            token:"Bearer " + token
        })
    })
})

router.post('/getinfo',function(req,res) {
    console.log(req.user);
    res.send({
        username:req.username
    })
})

module.exports=router